import user.UsersDAO;
import user.UsersImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 彬彬
 * 用于配置注册前后端，数据交互
 */
@WebServlet(name="RegisterServlet", urlPatterns = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String username = "";
        String password = "";
        String phone = "";
        username = req.getParameter("username");
        password = req.getParameter("password");
        phone = req.getParameter("phone");
        UsersDAO user = new UsersImpl();
//        System.out.println("hello:"+username+'\t'+password+"\t"+phone);
//        System.out.println(username.equals(""));
//        System.out.println(password.equals(""));
//        System.out.println(phone.equals(""));
        if(!(username.equals("") || password.equals("") || phone.equals(""))){
            if(user.Register(username, password, phone)){
                resp.getWriter().write("<script language='javascript'>alert('注册成功');" +
                        "window.location.href='./login.html';</script>");
                System.out.println("注册成功");
            }else{
                System.out.println("注册失败！！！");
                resp.getWriter().write("<script language='javascript'>alert('注册失败！');" +
                        "window.location.href='./login.html';</script>");
            }
        }else{
            System.out.println("请填写完整用户信息!!!");
            resp.getWriter().write("<script language='javascript'>alert('请填写完整用户信息!!!');" +
                    "window.location.href='./login.html';</script>");
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
